<?php
/**
 * This file is part of php-agenda.
 * 
 * php-agenda is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * php-agenda is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with php-agenda; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Copyright 2006, Thomas Abeel
 * 
 * Project: http://sourceforge.net/projects/php-agenda/
 * 
 */
?>
<?php
/*
 * Load languages
 */
include ('includes/php_language_detection.inc.php');
include ('lang/default.inc.php');
$langX = getLanguages();
foreach ($langX as $langY) {
	$filename = 'lang/' . $langY[0] . '.inc.php';
	if (file_exists($filename))
		include ($filename);
}

session_start();
if (isset ($_GET["logout"])) {
	unset ($_GET["logout"]);
	unset ($_SESSION["id"]);
	unset ($_SESSION["user"]);
	unset ($_SESSION["pass"]);
	header('Location: login.php');
	die();
}
?>
<?php
/*
 * Rest of the includes
 */
include ('adodb/adodb.inc.php');
if(file_exists('local_config_inc.php')){
	include ('local_config_inc.php');
}else{
	header("Location: install.php");
}
//include ('includes/secure_inc.php');
include ('includes/monthtable.inc.php');
include ('includes/version_control.php');
$db = ADONewConnection('mysql');
$db->Connect($dbhost, $dbuser, $dbpass, $dbname);
$db->debug = false;
include('includes/accesscontrol.inc.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
  <title><?php echo $lang["title"];?></title>
  <link href="style.css.php" rel="stylesheet" type="text/css"/>
  <link href="CalendarControl.css" rel="stylesheet" type="text/css"/>
  <link href="TimeControl.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<script src="CalendarControl.js.php" language="javascript"></script>
<script src="TimeControl.js.php" language="javascript"></script>
<?php

/*
 * ============================================================================
 * Busines logic
 */
$now = time();
$today = mktime(0, 0, 0, date("m", $now), date("d", $now), date("Y", $now));
$tomorrow = mktime(0, 0, 0, date("m", $now), date("d", $now) + 1, date("Y", $now));
$yesterday = mktime(0, 0, 0, date("m", $now), date("d", $now) - 1, date("Y", $now));
//current date
if (isset ($_GET["today"])) {
	$_SESSION["current"] = $today;
	unset ($_GET["today"]);
}
if (isset ($_GET["tomorrow"])) {
	$_SESSION["current"] = $tomorrow;
	unset ($_GET["tomorrow"]);
}
if (isset ($_GET["yesterday"])) {
	$_SESSION["current"] = $yesterday;
	unset ($_GET["yesterday"]);
}

if (isset ($_POST["goto"])) {
	$date=$_POST["date"];
	list($year,$month,$day)=split("-",$date);
	$current = mktime(0, 0, 0, $month, $day, $year);
} else
	if (isset ($_GET["day"])) {
		$current = $_GET["day"];
	} else {
		if (isset ($_SESSION["current"]))
			$current = $_SESSION["current"];
		else {
			$current = mktime(0, 0, 0, date("m", $now), date("d", $now), date("Y", $now));

		}
	}
$_SESSION["current"] = $current;
//events

if (isset ($_POST["newTODO"])) {
	$text = mysql_real_escape_string($_POST["text"]);
	$db->Execute("insert into todo (`user_id`,`priority`,`text`,`added`) " .
	"values(" . $_SESSION["id"] . "," . $_POST["priority"] . ",'$text'," . time() . ");");
	unset ($_POST["newTODO"]);
	unset ($_POST["priority"]);
	unset ($_POST["text"]);
}
if (isset ($_GET["deleteEvent"])) {
	$deleteid = mysql_real_escape_string($_GET["deleteEvent"]);
	$db->Execute("update events set status=1 where id=$deleteid and user_id=" . $_SESSION["id"]);
	unset ($_GET["deleteEvent"]);
}
if (isset ($_GET["deleteTODO"])) {
	$deleteid = mysql_real_escape_string($_GET["deleteTODO"]);
	$db->Execute("update todo set status=1 where id=$deleteid and user_id=" . $_SESSION["id"]);
	unset ($_GET["deleteTODO"]);
}
if (isset ($_GET["finishTODO"])) {
	$finishid = mysql_real_escape_string($_GET["finishTODO"]);
	$userid = $_SESSION["id"];
	$time = time();
	$db->Execute("update todo set status=2, closed=$time where id=$finishid and user_id=$userid");
	unset ($_GET["finishTODO"]);
}
if (isset ($_GET["finishEVENT"])) {
	$finishid = mysql_real_escape_string($_GET["finishEVENT"]);
	$userid = $_SESSION["id"];
	$time = time();
	$db->Execute("update events set status=2 where id=$finishid and user_id=$userid");
	unset ($_GET["finishEVENT"]);
}
if (isset ($_GET["newprior"])) {
	
	$newprior = mysql_real_escape_string($_GET["newprior"]);
	if ($newprior >= 0 and $newprior <= 5) {
		$tmp_id = mysql_real_escape_string($_GET["eventid"]);
		$userid = $_SESSION["id"];
		$db->Execute("update todo set priority=$newprior where id=$tmp_id and user_id=$userid");
	}
	unset ($_GET["newprior"]);
}

/*
 * ============================================================================
 */
?>

<!-- header div -->
<div id="hdr">
<h3 align="center"><?php echo $lang['title'];?></h3>
<div style="padding-left:10px;text-align: left;width:40%;float: left;">
<?php echo $lang['loggedin'];?>: <?php echo $_SESSION["user"];?>
</div>
<div style="padding-right:10px;text-align: right;width:40%;float:right;">
<?php
if (isset ($_GET['checkVersion'])) {
	versionCheck();
} else {
	echo '[<a href="?checkVersion">'.$lang['checkforupdates'].'</a>]';
}
?>
</div>
</div>
<!-- center column -->
<div id="c-block">
<div id="c-col">
&nbsp;
<?php

$prev = mktime(0, 0, 0, date("m", $current), date("d", $current) - 1, date("Y", $current));
$next = mktime(0, 0, 0, date("m", $current), date("d", $current) + 1, date("Y", $current));
$nextweek = mktime(0, 0, 0, date("m", $current), date("d", $current) + 7, date("Y", $current));
$nextmonth = mktime(0, 0, 0, date("m", $current) + 1, date("d", $current), date("Y", $current));
$nextyear = mktime(0, 0, 0, date("m", $current), date("d", $current), date("Y", $current) + 1);
$prevweek = mktime(0, 0, 0, date("m", $current), date("d", $current) - 7, date("Y", $current));
$prevmonth = mktime(0, 0, 0, date("m", $current) - 1, date("d", $current), date("Y", $current));
$prevyear = mktime(0, 0, 0, date("m", $current), date("d", $current), date("Y", $current) - 1);

echo "<h3 align=\"center\"><a href=\"?day=$prev\">" . $lang['time-previousday'] . "</a> | " . date("l, j F, Y", $current) . " | <a href=\"?day=$next\">" . $lang['time-nextday'] . "</a></h3>";
echo "<h4 align=\"center\"><a href=\"?yesterday\">" . $lang['time-yesterday'] . "</a> | <a href=\"?today\">" . $lang['time-today'] . "</a> | <a href=\"?tomorrow\">" . $lang['time-tomorrow'] . "</a></h4>";
echo "<div align=\"center\"><a href=\"?day=$prevyear\">&lt;&lt; " . $lang['time-year'] . "</a> | <a href=\"?day=$prevmonth\">&lt;&lt; " . $lang['time-month'] . "</a> | <a href=\"?day=$prevweek\">&lt;&lt; " . $lang['time-week'] . "</a> | <a href=\"?day=$nextweek\">" . $lang['time-week'] . " &gt;&gt;</a> | <a href=\"?day=$nextmonth\">" . $lang['time-month'] . " &gt;&gt;</a> | <a href=\"?day=$nextyear\">" . $lang['time-year'] . " &gt;&gt;</a></div>";
?>
<div align="center">
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" >
<label for="date"><?php echo $lang["date"];?> (yyyy-mm-dd):</label>
<input name="date" onfocus="showCalendarControl(this);" type="text"/>
<input type="submit" value="<?php echo $lang["index-goto"];?>" name="goto"/>
</form>
</div>
<?php


echo '<div class="box">';
echo '<h3>' . $lang["index-appointments"] . '</h3>';

//select events for today
$dayofweek = date("w", $current) + 1;
$dayofmonth = date("j", $current);
$dayofyear = date("z", $current) + 1;

$sql = "select distinct * from events where status!=1 and date>=$current and date<$next and user_id=" . $_SESSION["id"] . " order by dayevent desc,date asc ";
$recordXSet = $db->Execute($sql);
echo '<table width="100%">';
echo '<tr>'; 
while (!$recordXSet->EOF) {
	
	$eventid = $recordXSet->fields["id"];
	$deadline="";
	if($recordXSet->fields["deadline"]==1)
		$deadline='<img alt="deadline" src="img/deadline.gif"/>';
	if ($recordXSet->fields["dayevent"] == 0)
		echo '<td width="15%" align="right">'.$deadline . date("H:i", $recordXSet->fields["date"]) . '</td>';
	else
		echo '<td width="15%" align="right">'.$deadline.'<img alt="dayevent" src="img/clock.png"/></td>';
	echo '<td align="left"><span title="' . $recordXSet->fields["description"] . '">' . $recordXSet->fields["title"] . "</span></td>";
	echo '<td width="10%" align="right"><a href="edit_event.php?eventid='.$eventid.'"><img src="img/edit.png" alt="edit"/></a><a href="?deleteEvent='.$eventid.'" onClick="javascript:return confirm(\''.$lang['certainremove'].'\')"><img src="img/delete.png" alt="delete"/></a></td>';
	echo '</tr>';
	
	
	$recordXSet->MoveNext();
}
echo '<tr><td colspan=2><b>'.$lang['legend'].':</b><br/><img alt="dayevent" src="img/clock.png"/> '.$lang['legend-daylong'].'<br/><img alt="deadline" src="img/deadline.gif"/> '.$lang['legend-deadline'].'</td></tr>';
echo '</table>';
echo '</div>';
?>

<div class="box">
<h3><?php echo $lang["index-addevent"];?></h3>
<form action="engine/new_event.php" method="POST" >
<label for="date"><?php echo $lang["date"];?> (yyyy-mm-dd):</label>
<input name="date" onfocus="showCalendarControl(this);" type="text"/><br/>
<label for="time"><?php echo $lang["time"];?> (HH:MM):</label>
<input name="time" onfocus="showTimeControl(this);" type="text"/><br/>
<input type="checkbox" name="dayevent" value="dayevent"><?php echo $lang['add-daylong'];?></input><br/>
<input type="checkbox" name="deadline" value="deadline"><?php echo $lang['add-deadline'];?></input><br/>
<label for="title"><?php echo $lang['add-title'];?></label>
<input type="text" name="title" width=50/><br/>
<label for="description"><?php echo $lang['add-description'];?></label>
<input type="text" name="description" width=50/>
<br/>

<input type="submit" value="<?php echo $lang['add-button'];?>" name="newEvent"/>
</form>
</div>

<div class="box">
<h4><?php echo $lang['index-addtodo'];?></h4>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" >
<label for="priority"><?php echo $lang['todo-priority'];?></label>
<select name="priority" size="1">
	<option value="4"><?php echo $lang['todo-today'];?></option>
	<option value="5"><?php echo $lang['todo-urgent'];?></option>
	<option value="3"><?php echo $lang['todo-week'];?></option>
	<option value="2"><?php echo $lang['todo-month'];?></option>
	<option value="1"><?php echo $lang['todo-year'];?></option>
	<option value="0"><?php echo $lang['todo-sometime'];?></option>
</select>
<label for="text"><?php echo $lang['todo-description'];?></label>
<input type="text" name="text" width=50/>
<input type="submit" value="<?php echo $lang['todo-button'];?>" name="newTODO"/>
</form>
</div>

<div class="box">
<h4><?php echo $lang['nextevents'];?></h4>
<?php
$sql = "select distinct * from events where status!=1 and date>=$today and user_id=" . $_SESSION["id"] . " order by date asc limit 50";
$recordXSet = $db->Execute($sql);
echo '<table>';
while (!$recordXSet->EOF) {
	$nextid = $recordXSet->fields["id"];

	echo '<tr>';
	if ($recordXSet->fields["dayevent"] == 0)
		echo '<td width="25%" align="left">' . date("j-m-y  H:i", $recordXSet->fields["date"]) . '</td>';
	else
		echo '<td align="left">' . date("j-m-y", $recordXSet->fields["date"]) . '</td>';
	echo '<td align="left"><span title="' . $recordXSet->fields["description"] . '">' . $recordXSet->fields["title"] . "</span></td>";
	echo "<td width=\"5%\" align=\"right\"><a href=\"?deleteEvent=$nextid\" onClick=\"javascript:return confirm('".$lang['certainremove']."')\"><img src=\"img/delete.png\"/ alt=\"delete\"/></a></td>";
	echo '</tr>';

	$recordXSet->MoveNext();
}
echo '</table>';
?>
</div>
<div class="box" align="center"><a href="?logout"><?php echo $lang['logout'];?></a> | 
<?php
if ($_SESSION[id] == 1) {
	echo '<a href="register.php">'.$lang['register'].'</a> | ';
}
?><a href="mailto:thomas@abeel.be"><?php echo $lang['contact'];?></a></div>
&nbsp;
</div>
<!-- end of center column -->
</div>
<!-- end c-block -->
<!-- ===================== FOOTER ========================= -->
<div id="ftr" align="center">
	<?php include('includes/footer.php');?>
</div>
<!-- ====================== LEFT ========================== -->
<!-- left column -->
<div id="lh-col">
<div class="box">
<table>
<?php


echo '<tr><td colspan=7 align="center"><b>'.$lang['left-coming'].'</b></td></tr>';
echo '<tr>';
for ($i = 0; $i < 7; $i++) {
	$xcolor = '#ffffff';
	$tmp = mktime(0, 0, 0, date("m", $now), date("d", $now) + $i, date("Y", $now));
	$tmp1 = mktime(0, 0, 0, date("m", $now), date("d", $now) + $i +1, date("Y", $now));

	$sql = "select * from events where status!=1 and date>=$tmp and date<$tmp1 and user_id=" . $_SESSION["id"] . " order by date asc";
	$recordSet = & $db->Execute($sql);
	$count = 0;
	while (!$recordSet->EOF) {
		$count++;
		$recordSet->MoveNext();

	}
	if ($count > 0) {
		$xcolor = '#ff0000';
	}
	if (date("w", $tmp) == 0 or date("w", $tmp) == 6)
		echo "<td><span style=\"background: $xcolor ;\"><b><a href=\"?day=$tmp\">" . date("j", $tmp) . "</a></b></span></td>";
	else
		echo "<td><span style=\"background: $xcolor ;\"><a href=\"?day=$tmp\">" . date("j", $tmp) . "</a></span></td>";
}
echo '</tr>';
?>
</table>
</div>
<!-- THIS MONTH -->
<div class="box">
<?php
printmonthtable(mktime(0, 0, 0, date("m", $today), 1, date("y", $today)), $db);
?>
</div>
<!-- NEXT MONTH -->
<div class="box">
<?php
printmonthtable(mktime(0, 0, 0, date("m", $today) + 1, 1, date("y", $today)), $db);
?>
</div>
<div class="box">
<?php
printmonthtable(mktime(0, 0, 0, date("m", $today) + 2, 1, date("y", $today)), $db);
?>
</div>
</div>
<!-- end of left column -->
<!-- ======================= RIGHT ================================ -->
<!-- right column -->
<div id="rh-col"><br />
<div class="box">
<h4 align="center"><?php echo $lang['right-deadline'];?></h4>
<?php


$sql = "select * from events where status=0 and deadline=1 and user_id=" . $_SESSION["id"] . " order by date asc limit 10 ";
$recordSet = & $db->Execute($sql);
echo '<table>';
while (!$recordSet->EOF) {
	echo '<tr>';
	$deadid = $recordSet->fields["id"];
	$dtime = $recordSet->fields["date"];
	$days = (int) (($dtime - $today) / (60 * 60 * 24));
	echo "<td><a onClick=\"javascript:return confirm('".$lang['certainfinish']."')\" href=\"?finishEVENT=$deadid\"><img height='100%' src=\"img/finished.png\"/ alt=\"finish\"/></a></td>";
	echo '<td>' . date("j-m-y", $dtime) . '(' . $days . ') </td><td><span title="' . $recordSet->fields["description"] . '">' . $recordSet->fields["title"] . '</span></td>';
	echo '</tr>';
	$recordSet->MoveNext();
}
echo '</table>'
?>
</div>
<div class="box">
<h4 align="center"><?php echo $lang['right-todo'];?></h4>
<?php
 $sql = "select * from todo where status=0 and user_id=" . $_SESSION["id"] . " order by priority desc";
$recordSet = & $db->Execute($sql);
while (!$recordSet->EOF) {
	echo '<div class="todo">';
	$todoid = $recordSet->fields["id"];

	$priority = $recordSet->fields["priority"];

	$color[] = "#cceedd";
	$color[] = "#c8c8ff";
	$color[] = "#ffffb0";
	$color[] = "#ffd850";
	$color[] = "#ff50a8";
	$color[] = "#ff0000";

	echo "<div style=\"background-color:" . $color[$priority] . "\"; width: 100%>";
	if ($priority < 5)
		$upprior = $priority +1;
	else
		$upprior = $priority;

	if ($priority > 0)
		$downprior = $priority -1;
	else
		$downprior = $priority;

	echo "<a onClick=\"javascript:return confirm('".$lang['certainfinish']."')\" href=\"?finishTODO=$todoid\"><img height='100%' src=\"img/finished.png\"/ alt=\"finish\"/></a>";
	echo "<a onClick=\"javascript:return confirm('".$lang['certainremove']."')\" href=\"?deleteTODO=$todoid\"><img height='100%' src=\"img/deleteTODO.png\"/ alt=\"delete\"/></a>";
	echo "<a href=\"?newprior=$upprior&eventid=$todoid\"><img height='100%' src=\"img/uparrow.png\"/ alt=\"up\"/></a>";
	echo "<a href=\"?newprior=$downprior&eventid=$todoid\"><img height='100%' src=\"img/downarrow.png\"/ alt=\"down\"/></a>";

	echo " " . $recordSet->fields["text"];

	echo "</div>";
	echo '</div>';
	$recordSet->MoveNext();
}
?>

</div>

<div class="box">
<h4 align="center"><?php echo $lang['right-recently'];?></h4>
<?php
$sql = "select * from todo where status=2 and user_id=" . $_SESSION["id"] . " order by closed desc limit 10";
$recordSet = & $db->Execute($sql);
echo "<ul>";
while (!$recordSet->EOF) {
	echo '<div class="todo">';
	$recentid = $recordSet->fields["id"];
	$priority = $recordSet->fields["priority"];
	echo "<div width: 100%>";
	echo "<li>" . $recordSet->fields["text"] . "</li>";
	echo "</div>";
	echo '</div>';
	$recordSet->MoveNext();
}
echo '</ul>'
?>

</div>
</div>
</div>
<!-- end of right column -->
</body>
</html>
<?php
 $db->Close();
?>

